package com.gaoyi.crm.vm;

import android.app.Application;
import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.gaoyi.crm.R;
import com.gaoyi.crm.activity.MainActivity;
import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.SPName;
import com.gaoyi.crm.db.bean.LoginBean;
import com.gaoyi.crm.db.dto.LoginDto;
import com.gaoyi.crm.db.dto.UserDto;
import com.gaoyi.crm.repository.UserRepository;
import com.gaoyi.crm.util.LanguageUtils;
import com.gaoyi.crm.util.RSAUtil;
import com.gaoyi.crm.util.SPUtil;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.vm.base.BaseViewModel;
import com.google.gson.reflect.TypeToken;

public class LoginViewModel extends BaseViewModel {

    MutableLiveData<String> mName;
    MutableLiveData<String> mPassword;
    MutableLiveData<Integer> mLoginResultCode;
    UserRepository mRepository;

    public LoginViewModel(@NonNull Application application) {
        super(application);
        mRepository = new UserRepository();
    }

    public MutableLiveData<String> getName() {
        return mName;
    }

    public void setName(String name) {
        mName = new MutableLiveData<>();
        mName.setValue(name);
    }

    public MutableLiveData<String> getPassword() {
        if (mPassword == null) {
            mPassword = new MutableLiveData<>();
        }
        return mPassword;
    }

    public MutableLiveData<Integer> getLoginResultCode() {
        if (mLoginResultCode == null) {
            mLoginResultCode = new MutableLiveData<>();
        }
        return mLoginResultCode;
    }

    public void login(String pwd) {
        LoginBean bean = new LoginBean();
        bean.setUserName(mName.getValue());
        bean.setPassword(pwd);
        mRepository.login(bean, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                ApiResultData<LoginDto> apiResultData = (ApiResultData<LoginDto>) apiResult;
                LoginDto data = apiResultData.getData();
                mLoginResultCode.setValue(apiResultData.getStatus());
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    if (data != null) { // 判断是否是回传公钥
                        String publicKey = data.getPublicKey();
                        if (!TextUtils.isEmpty(publicKey)) {
                            login(RSAUtil.encrypt(mPassword.getValue(), publicKey));
                            return;
                        }
                    }
                    ToastUtil.showToast(apiResultData.getMessage());
                    return;
                }
                UserDto userInfo = data.getUserInfo();
                SPUtil.getInstance().putString(SPName.USER, userInfo.getUserName());
                SPUtil.getInstance().putString(SPName.ID, userInfo.getId());
                SPUtil.getInstance().putString(SPName.TOKEN,
                        data.getToken() == null ? "" : data.getToken());
                SPUtil.getInstance().putString(SPName.REFRESH_TOKEN,
                        data.getRefreshToken() == null ? "" : data.getRefreshToken());
                try {
                    SPUtil.getInstance().putInt(SPName.LANGUAGE,
                            userInfo.getLang() == null ? LanguageUtils.LANG_ENGLISH :
                                    Integer.parseInt(userInfo.getLang().getId()));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

                Intent intent = new Intent(getApplication(), MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                mApplication.startActivity(intent);
            }

            @Override
            public void onFails(Exception e) {
                mLoginResultCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<LoginDto>>() {
        }.getType());
    }
}
